<template>
  <div class="notification-container">
    <SidebarNav />
    <div class="main-content">
      <TopHeader />
      <div class="content-area">
        <h2 class="page-title">通知</h2>
        
        <!-- 日期查询功能 -->
        <div class="date-search-bar">
          <div class="date-picker">
            <input type="date" v-model="startDate" placeholder="开始日期" />
          </div>
          <div class="date-separator">至</div>
          <div class="date-picker">
            <input type="date" v-model="endDate" placeholder="结束日期" />
          </div>
          <button class="search-btn" @click="searchByDate">查找</button>
          <button class="mark-all-read-btn" @click="markAllAsRead">一键已读</button>
        </div>
        
        <div class="notification-list">
          <div 
            class="notification-item" 
            v-for="item in paginatedNotifications" 
            :key="item.id"
            @mouseenter="showBiddingFile(item)"
            @mouseleave="hideBiddingFile"
            @click="markAsRead(item)"
          >
            <div class="notification-icon">
              <img src="../assets/icons/通知.svg" alt="通知" />
              <div class="notification-dot" v-if="!item.read"></div>
            </div>
            <div class="notification-content">
              <div class="notification-title">{{ item.title }}</div>
              <div class="notification-message">{{ item.message }}</div>
              <div class="notification-time">{{ item.time }}</div>
            </div>
            <!-- 悬停时显示的招标文件名称 -->
            <div class="bidding-file-tooltip" v-if="hoveredItem === item.id">
              {{ item.biddingFile }}
            </div>
          </div>
          
          <div class="empty-notifications" v-if="filteredNotifications.length === 0">
            <p>暂无通知</p>
          </div>
        </div>
        
        <!-- 分页控件 -->
        <div class="pagination" v-if="filteredNotifications.length > 0">
          <button class="prev-btn" :disabled="currentPage <= 1" @click="prevPage">&lt;</button>
          <div class="page-numbers">
            <span 
              v-for="page in displayedPages" 
              :key="page" 
              :class="{ 'active': currentPage === page }"
              @click="goToPage(page)"
            >
              {{ page }}
            </span>
          </div>
          <button class="next-btn" :disabled="currentPage >= totalPages" @click="nextPage">&gt;</button>
        </div>
      </div>
    </div>
    <tender-progress-assistant />
  </div>
</template>

<script>
import { ref, computed, inject, watch } from 'vue';
import SidebarNav from '../components/Sidebar.vue';
import TopHeader from '../components/TopHeader.vue';
import TenderProgressAssistant from '@/components/TenderProgressAssistant.vue';

export default {
  name: 'NotificationPage',
  components: {
    SidebarNav,
    TopHeader,
    TenderProgressAssistant,
  },
  setup() {
    // 获取全局通知数据
    const globalNotifications = inject('globalNotifications');
    
    // 分页设置
    const currentPage = ref(1);
    const pageSize = ref(15);
    
    // 日期过滤
    const startDate = ref('');
    const endDate = ref('');
    const shouldFilter = ref(false);
    
    // 悬停状态
    const hoveredItem = ref(null);
    
    // 计算未读通知数量
    const unreadCount = computed(() => globalNotifications.unreadCount.value);
    
    // 根据日期过滤通知
    const filteredNotifications = computed(() => {
      if (!shouldFilter.value || (!startDate.value && !endDate.value)) 
        return globalNotifications.notifications.value;
      
      return globalNotifications.notifications.value.filter(item => {
        const notificationDate = new Date(item.time);
        const start = startDate.value ? new Date(startDate.value) : null;
        const end = endDate.value ? new Date(endDate.value) : null;
        
        if (start && end) {
          return notificationDate >= start && notificationDate <= end;
        } else if (start) {
          return notificationDate >= start;
        } else if (end) {
          return notificationDate <= end;
        }
        
        return true;
      });
    });
    
    // 计算总页数
    const totalPages = computed(() => {
      return Math.ceil(filteredNotifications.value.length / pageSize.value);
    });
    
    // 当前页的数据
    const paginatedNotifications = computed(() => {
      const start = (currentPage.value - 1) * pageSize.value;
      const end = start + pageSize.value;
      return filteredNotifications.value.slice(start, end);
    });
    
    // 显示的页码范围
    const displayedPages = computed(() => {
      const pages = [];
      const maxVisiblePages = 5;
      
      let startPage = Math.max(1, currentPage.value - Math.floor(maxVisiblePages / 2));
      let endPage = Math.min(totalPages.value, startPage + maxVisiblePages - 1);
      
      if (endPage - startPage + 1 < maxVisiblePages) {
        startPage = Math.max(1, endPage - maxVisiblePages + 1);
      }
      
      for (let i = startPage; i <= endPage; i++) {
        pages.push(i);
      }
      
      return pages;
    });
    
    // 监听过滤条件变化，重置页码
    watch([startDate, endDate], () => {
      // 不再自动触发过滤，由用户点击查找按钮触发
    });
    
    // 分页方法
    const prevPage = () => {
      if (currentPage.value > 1) {
        currentPage.value--;
      }
    };
    
    const nextPage = () => {
      if (currentPage.value < totalPages.value) {
        currentPage.value++;
      }
    };
    
    const goToPage = (page) => {
      currentPage.value = page;
    };
    
    // 日期查询
    const searchByDate = () => {
      shouldFilter.value = true;
      currentPage.value = 1;
    };
    
    // 显示/隐藏招标文件名称
    const showBiddingFile = (item) => {
      hoveredItem.value = item.id;
    };
    
    const hideBiddingFile = () => {
      hoveredItem.value = null;
    };
    
    // 标记通知为已读
    const markAsRead = (item) => {
      if (!item.read) {
        globalNotifications.markAsRead(item.id);
      }
    };
    
    // 标记所有通知为已读
    const markAllAsRead = () => {
      globalNotifications.markAllAsRead();
    };
    
    return {
      filteredNotifications,
      paginatedNotifications,
      currentPage,
      pageSize,
      totalPages,
      displayedPages,
      startDate,
      endDate,
      hoveredItem,
      unreadCount,
      prevPage,
      nextPage,
      goToPage,
      searchByDate,
      showBiddingFile,
      hideBiddingFile,
      markAsRead,
      markAllAsRead
    };
  }
};
</script>

<style scoped>
.notification-container {
  display: flex;
  min-height: 100vh;
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.content-area {
  flex: 1;
  padding: 20px 30px;
}

.page-title {
  color: #515151;
  font-size: 24px;
  margin-bottom: 35px;
}

/* 日期查询栏 */
.date-search-bar {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  gap: 15px;
}

.date-picker {
  position: relative;
}

.date-separator {
  font-size: 14px;
  color: #515151;
  display: flex;
  align-items: center;
}

.date-picker input {
  padding: 10px 15px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  outline: none;
  font-size: 14px;
  background-color: #ECF0F1;
  width: 150px;
}

.date-picker input::placeholder {
  color: #515151;
}

.search-btn {
  padding: 10px 20px;
  background-color: #71D9DA;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}

.mark-all-read-btn {
  padding: 10px 20px;
  background-color: #009688;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  margin-left: auto;
}

.notification-list {
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 10px;
  margin-bottom: 20px;
  position: relative;
}

.notification-item {
  display: flex;
  padding: 15px;
  border-bottom: 1px solid #e0e0e0;
  transition: background-color 0.3s;
  position: relative;
  cursor: pointer;
}

.notification-item:last-child {
  border-bottom: none;
}

.notification-item:hover {
  background-color: #f5f7fa;
}

.notification-icon {
  position: relative;
  margin-right: 15px;
  display: flex;
  align-items: center;
}

.notification-icon img {
  width: 24px;
  height: 24px;
}

.notification-dot {
  position: absolute;
  top: 0px;
  right: -2px;
  width: 8px;
  height: 8px;
  background-color: red;
  border-radius: 50%;
}

.notification-content {
  flex: 1;
}

.notification-title {
  font-weight: bold;
  margin-bottom: 5px;
  color: #303133;
}

.notification-message {
  color: #606266;
  margin-bottom: 8px;
  line-height: 1.5;
}

.notification-time {
  color: #909399;
  font-size: 12px;
}

/* 招标文件名称提示 */
.bidding-file-tooltip {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  top: 0;
  width: 60%;
  background-color: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 8px;
  border-radius: 4px;
  z-index: 10;
  font-size: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  text-align: left;
}

.empty-notifications {
  padding: 40px;
  text-align: center;
  color: #909399;
}

/* 分页样式 */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
}

.prev-btn, .next-btn {
  padding: 5px 10px;
  border: 1px solid #dcdfe6;
  background-color: white;
  cursor: pointer;
  border-radius: 5px;
}

.prev-btn:disabled, .next-btn:disabled {
  cursor: not-allowed;
  color: #c0c4cc;
}

.page-numbers {
  display: flex;
  margin: 0 10px;
}

.page-numbers span {
  padding: 5px 10px;
  margin: 0 5px;
  border: 1px solid #dcdfe6;
  cursor: pointer;
  border-radius: 5px;
}

.page-numbers span.active {
  background-color: #409eff;
  color: white;
  border-color: #409eff;
}
</style> 